<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/3
 * Time: 22:04
 */

namespace base;

use OSS\Core\OssException;
use OSS\OssClient;

class OssApi
{

    /**
     * ACCESS_KEY_ID
     */
    const ACCESS_KEY_ID = \OssConfig::OSS_ACCESS_ID;

    /**
     * ACCESS_KEY_ID
     */
    const ACCESS_KEY_SECRET = \OssConfig::OSS_ACCESS_KEY;

    /**
     * ENDPOINT
     */
    const ENDPOINT = \OssConfig::OSS_ENDPOINT;

    /**
     * BUCKET
     */
    const BUCKET = \OssConfig::OSS_BUCKET;

    /**
     * 单例对象
     * @var
     */
    private static $_instance;


    /**
     * 构造函数
     * Oss constructor.
     */
    private function __construct()
    {

    }

    /**
     * 克隆
     */
    private function __clone()
    {

    }

    /**
     * 获取一个OssClient实例
     * @return null|OssClient
     */
    public static function getInstance()
    {
        if (!(self::$_instance instanceof OssClient)) {
            try {
                self::$_instance = new OssClient(self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET, self::ENDPOINT, false);
            } catch (OssException $e) {
                printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
                printf($e->getMessage() . "\n");
                return null;
            }
        }
        return self::$_instance;
    }

    /**
     * 获取bucket
     * @return string
     */
    public static function getBucketName()
    {
        return self::BUCKET;
    }

}